/**
 * @class   wosh::DiscoveryUdpMessage
 * @brief   Represent a Discovery UDP message. Provides (de)serialization.
 *
 *
 * @see     wosh::services::DiscoveryUdpBundle
 * @see     DiscoveryUdpImpl
 * @see     wosh::WoshHost
 ****************************************************************************
 * @version 1.01 $Id: DiscoveryUdpMessage.h 2258 2010-04-02 13:02:21Z alex $
 * @date    Released Stable on 2010-02-21
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Bundles_Services_DiscoveryUdpMessage_H__
 #define __WOSH_Bundles_Services_DiscoveryUdpMessage_H__

 #include <woshDefs.h>
 #include <string>
 #include <vector>
 #include <map>


namespace wosh {

#define _DiscoveryUdpMessage_KernelName_MAXSIZE		64
#define _DiscoveryUdpMessage_BindAddress_MAXSIZE	32

#define _DiscoveryUdpMessage_ProtocolVersion		'T'

/**
 * @brief   Discovery packet header
 */
struct DiscoveryUdpMessage_data {
	char protocol_version;		///< protocol version as short

	char kernel_name[_DiscoveryUdpMessage_KernelName_MAXSIZE]; ///< the name of the kernel
	char kernel_type;			///< type of the kernel
	char kernel_state;			///< protocol version as short integer, see WoshKernel::KERNEL_STATE

	unsigned int notifyFreq;	///< notification frequency (of discovery service) in seconds
	char bind_address[_DiscoveryUdpMessage_BindAddress_MAXSIZE]; ///< bind address of discovery service
};


class DiscoveryUdpMessage {

	public:
		static const int MaxLength = 1023;	///< maximum size of the message

	public:
		/**
		 * @brief   Empty constructor, init vars.
		 */
		DiscoveryUdpMessage() {
			clear();
		 }
		/**
		 * @brief   Empty destructor.
		 */
		virtual ~DiscoveryUdpMessage() { }


/** @name Serialization
 * @{
 ******************************************************************************/
 	public:

		/**
		 * @brief   Deserialize the message from plain text.
		 * @param [out] data buffer to write to, must be allocated, see maxSize
		 * @param [in] maxSize the size of the buffer
		 * @return WRET_ERR_PARAM if message is malformed or truncated; WRET_OK else
		 */
		WRESULT loadFrom( const char* data, int maxSize );

		/**
		 * @brief   Serialize the message to plain text.
		 * @param [in] data buffer to write to, must be allocated, see maxSize
		 * @param [in] maxSize the size of the buffer
		 * @return number of bytes
		 */
		int saveTo( char* data, int maxSize = -1 );

		/**
		 * @brief   Clear all fields.
		 */
		void clear();

//@}
/** @name Getters
 * @{
 ******************************************************************************/
 	public:
		std::string getKernelName() const					{ return std::string( this->dd_data.kernel_name ); }
		unsigned short getKernelState() const				{ return (int)this->dd_data.kernel_state; }
		unsigned short getKernelType() const				{ return (int)this->dd_data.kernel_type; }

		unsigned int getNotifyFrequency() const				{ return this->dd_data.notifyFreq; }

 		const std::string& getSourceHost() const			{ return this->sourceHost; }

		short getProtocolVersion() const					{ return (short)this->dd_data.protocol_version; }

		std::string getDiscoveryBindAddress() const			{ return std::string( this->dd_data.bind_address ); }

		int getSupportedProtocols( std::vector<std::string>& protocols ) const;

		int getNeighbours( std::map<std::string, long>& neighb ) const;


		struct DiscoveryUdpMessage_data& getStaticData()		{ return this->dd_data; }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
 	public:
		bool setKernelName( const std::string& value );
		void setKernelState( unsigned short value )			{ this->dd_data.kernel_state = (char)value; }
		void setKernelType( unsigned short value )			{ this->dd_data.kernel_type = (char)value; }

		void setSourceHost( const std::string& value )		{ this->sourceHost = value; }

		void setNotifyFrequency( unsigned int value )		{ this->dd_data.notifyFreq = value; }

		void setProtocolVersion( short value )				{ this->dd_data.protocol_version = (char)value; }

		void setSupportedProtocols( const std::vector<std::string>& protocols );

		bool setDiscoveryBindAddress( const std::string& value );

		void setNeighbours( const std::map<std::string, long>& neighb );

//@}
	protected:
		struct DiscoveryUdpMessage_data dd_data;	///< packet header
		std::string supportedProtocols;				///< protocols supported
		std::string neighbours;						///< list of neighbours (comma separated)

		std::string sourceHost;						///< hold for convenience:


}; // class def

}; // namespace wosh

#endif //__WOSH_Bundles_Services_DiscoveryUdpMessage_H__
